package com.gss.stockLedger.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.gss.stockLedger.domain.StockTransactionDtls;
import com.gss.stockLedger.util.ApplicationConstants;

@Repository
public class QueryVendorWiseDAO {

	@Autowired
	SessionFactory sessionFactory;
	
	@Autowired
	SessionFactory factory2;
	
	public List getVendorList(String companyName, String vendorCode) {
		Session session = null;
		Transaction transaction = null;
		
		try{
			if(companyName.equalsIgnoreCase("RNAIPL")){
				session = sessionFactory.openSession();
			}
			else if(companyName.equalsIgnoreCase("NMIPL")){
				session = factory2.openSession();
			}
			
			transaction = session.beginTransaction();
			
			ProjectionList projectionList = Projections.projectionList();
			Projection projection = Projections.distinct(Projections.property("vendorCode"));
			projectionList.add(projection);
			projectionList.add(Projections.property("vendorName"));
					
			Criteria criteria = session.createCriteria(StockTransactionDtls.class).setProjection(projectionList);
			
			if(vendorCode!=null && !vendorCode.equalsIgnoreCase("All")){
				criteria.add(Restrictions.eq("vendorCode", vendorCode));
			}
			
			return criteria.list();
		}
		catch(Throwable e){
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(null != transaction)
				transaction.commit();
			if(session!=null&&session.isOpen())
			{
				session.close();
				//session = null;
			}
		}
	}

	public List<StockTransactionDtls> getVendorWisePurchaseReport(String companyName, Date endDate, String vendorCode) {
		Session session = null;
		Transaction transaction = null;
		
		try{
			if(companyName.equalsIgnoreCase("RNAIPL")){
				session = sessionFactory.openSession();
			}
			else if(companyName.equalsIgnoreCase("NMIPL")){
				session =  factory2.openSession();
			}
			
			transaction = session.beginTransaction();

			/*StringBuffer query_Buffer = new StringBuffer();
			query_Buffer.append("select" +
					" trnx.inventoryType, trnx.partId, trnx.partDesc, trnx.sapDocNo, trnx.sapEntryDate," +
					" trnx.assignmentId, trnx.transactionDate, trnx.invoiceNo, trnx.strInvoiceDate," +
					" trnx.vendorCode, trnx.vendorName, trnx.valueFCY, trnx.unitFCY, trnx.purchaseType," +
					" trnx.transactionType, trnx.acctType, sum(trnx.quantity), sum(trnx.value)" +
					" from StockTransactionDtls as trnx" +
					" where trnx.transactionType = '"+ApplicationConstants.transactionTypeVendorReport+"'");
							
			if(null != vendorCode && !"".equals(vendorCode)){
				query_Buffer.append(" AND trnx.vendorCode = :vendorCode");
			}
			if(null != endDate && !"".equals(endDate)){
				query_Buffer.append(" AND trnx.transactionDate = :transactionDate");
			}
			
			query_Buffer.append(" group by trnx.vendorCode, trnx.partId");
			
			Query query = session.createQuery(query_Buffer.toString());
			
			if(null != vendorCode && !"".equals(vendorCode)){
				query.setParameter("vendorCode", vendorCode);
			}
			if(null != endDate){
				query.setParameter("transactionDate", endDate);
			}*/
			
			Criteria criteria = session.createCriteria(StockTransactionDtls.class).add(Restrictions.eq("vendorCode", vendorCode))
					.add(Restrictions.eq("transactionType", ApplicationConstants.transactionTypeVendorReport));
			
			if(endDate!=null)
				criteria.add(Restrictions.le("transactionDate", endDate));
			
//			List<StockTransactionDtls> stockTransactionDtls = query.list();
			return criteria.list();
		}
		catch (Throwable e) {
			e.printStackTrace();
			return null;
		}
		finally
		{
			if(null != transaction)
				transaction.commit();
			if(session!=null&&session.isOpen())
			{
				session.close();
				//session = null;
			}
		}
	}

	

}
